﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：省份，城市，区域管理
  //创建人：赵国伟
  //生成时间：2012/04/22 17:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.DAL;
using Shopping.Model;
using Shopping.Common;

namespace Shopping.BLL
{
    public class ProvinceCityAreasManage
    {
        private readonly DALProvinces dalProvince = new DALProvinces();
        private readonly DALCities dalCity = new DALCities();
        private readonly DALAreas dalArea = new DALAreas();

        #region "保存省份信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 保存省份信息；
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ReturnValue SaveProvince(Provinces p)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"保存失败！");
            //检查名字是否重复；
            if (dalProvince.QueryList(1, "ProvinceName ='" + p.ProvinceName + "' and ProvinceID<>" + p.ProvinceID, "") != null)
            {
                ret.Message = "省份名称重复!";
                return ret;
            }
            int result = 0;
            if (p.ProvinceID > 0)
            {
                Provinces o = dalProvince.GetModel(p.ProvinceID);
                p.CreateDate = o.CreateDate;
                p.LastModifyDate = DateTime.Now;
                result = dalProvince.Update(p);
            }
            else
            {
                p.LastModifyDate = DateTime.Now;
                p.CreateDate = DateTime.Now;
                result = dalProvince.Add(p);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "删除一条指定的省份信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 删除一条指定的省份信息；
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public ReturnValue DeleteProvince(int provinceId)
        {
            ReturnValue ret = new ReturnValue();
            if (dalProvince.Delete(provinceId) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }
            return ret;
        }
        #endregion

        #region "查询一条指定的省份信息 by 赵国伟  2012-04-02"
        /// <summary>
        /// 查询一条指定的省份信息
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public Provinces GetProvinceInfo(int provinceId)
        {
            return dalProvince.GetModel(provinceId);
        }
        #endregion

        #region "查询所有的省份信息； by 赵国伟 2012-04-03"
        /// <summary>
        /// 查询所有的省份信息；
        /// </summary>
        /// <returns></returns>
        public List<Provinces> QueryProvinces()
        {
            return dalProvince.QueryList("", "OrderValue,ProvinceID desc");
        }
        #endregion

        #region "查询所有的省份信息；并存为Hash结构； by 赵国伟 2012-04-03"
        /// <summary>
        /// 查询所有的省份信息；并存为Hash结构；
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> QueryProvincesToHash()
        {
            List<IDNameObj> listOfData = dalProvince.Find();
            return BLLHelper.GenerateToHash(listOfData);
        }
        #endregion

        #region "查询所有的省份信息 并存为下拉选项； by 赵国伟 2012-04-03"
        /// <summary>
        /// 查询所有的省份信息 并存为下拉选项；
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryProvincesToSelectOption(string defaultValue)
        {
            ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
            return BLLHelper.GenerateToSelectOption(bllArea.dalProvince.Find(), defaultValue);
        }
        #endregion




        #region "保存城市信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 保存城市信息；
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public ReturnValue SaveCity(Cities c)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败！");
            //检查名字是否重复；
            if (dalCity.QueryList(1, "CityName ='" + c.CityName + "' and CityID<>" + c.CityID, "") != null)
            {
                ret.Message = "城市名称重复!";
                return ret;
            }
            int result = 0;
            if (c.CityID > 0)
            {
                Cities o = dalCity.GetModel(c.CityID);
                c.CreateDate = o.CreateDate;
                c.LastModifyDate = DateTime.Now;
                result = dalCity.Update(c);
            }
            else
            {
                c.LastModifyDate = DateTime.Now;
                c.CreateDate = DateTime.Now;
                result = dalCity.Add(c);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "删除一条指定的城市信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 删除一条指定的城市信息；
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public ReturnValue DeleteCity(int cityId)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"删除失败！");
            if (dalCity.Delete(cityId) > 0)
            {
                ret.Message = "删除成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询一条指定的城市信息 by 赵国伟  2012-04-02"
        /// <summary>
        /// 查询一条指定的城市信息
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public Cities GetCityInfo(int cityId)
        {
            return dalCity.GetModel(cityId);
        }
        #endregion

        #region "分页返回指定条件下的城市信息； by 赵国伟 2012-04-03"
        /// <summary>
        /// 分页返回指定条件下的城市信息；
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="ProvinceId"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<CityInfo> QueryCitiesAsPage(string keyWords, int ProvinceId, AsPage p)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(keyWords))
                cond += " and CityName like '%" + keyWords + "%' ";
            if (ProvinceId > 0)
                cond += " and ProvinceID=" + ProvinceId;
            string orderby = "OrderValue,CityID desc";

            List<Cities> listOfSource = dalCity.QueryListAsPage(cond, orderby, p.CurPage, p.PageSize, out p.TotalCount);
            List<CityInfo> listOfTarget = null;
            if (listOfSource != null)
            {
                listOfTarget = new List<CityInfo>();
                Dictionary<int, string> dictProvinces = QueryProvincesToHash();
                foreach (Cities source in listOfSource)
                {
                    CityInfo target = new CityInfo();
                    target.City = source;
                    target.ProvinceName = dictProvinces[source.ProvinceID];
                    listOfTarget.Add(target);
                }
            }
            return listOfTarget;
        }
        #endregion

        #region "查询指定城市的Hash结构； by 赵国伟 2012-04-03"
        /// <summary>
        /// 查询指定城市的基本信息并存为Hash结构；
        /// </summary>
        /// <param name="cityIds"></param>
        /// <returns></returns>
        public Dictionary<int,string> QueryCitiesToHash(List<string> cityIds)
        {
            string cond = " ";
            if (cityIds != null && cityIds.Count > 0)
                cond = " CityID in('" + string.Join("','", cityIds.ToArray()) + "') ";

            List<IDNameObj> listOfData = dalCity.Find(cond, "");
            return BLLHelper.GenerateToHash(listOfData);
        }
        #endregion

        #region "返回指定城市与省份的对应关系，并存为Hash结构； by 赵国伟 2012-04-03"
        /// <summary>
        /// 返回指定城市与省份的对应关系，并存为Hash结构；
        /// </summary>
        /// <param name="cityIds"></param>
        /// <returns></returns>
        public Dictionary<int, int> QueryCityProvinceMapToHash(List<string> cityIds)
        {
            string cond = " ";
            if (cityIds != null && cityIds.Count > 0)
                cond = " CityID in('" + string.Join("','", cityIds.ToArray()) + "') ";

            Dictionary<int, int> dict = new Dictionary<int, int>();
            List<Cities> listOfData = dalCity.QueryList(cond, "");
            if (listOfData != null)
            {
                foreach (Cities data in listOfData)
                    dict.Add(data.CityID, data.ProvinceID);
            }
            return dict;
        }
        #endregion

        #region "查询指定省份下的城市信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 查询指定省份下的城市信息；
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryCities(int provinceId)
        {
            string cond = "ProvinceID=" + provinceId;
            return dalCity.Find(cond, "OrderValue,CityID");
        }
        #endregion

        #region "查询指定省份下的城市并输出为下拉选项； by 赵国伟 2012-04-03"
        /// <summary>
        /// 查询指定省份下的城市并输出为下拉选项；
        /// </summary>
        /// <param name="ProvinceId"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryCitiesToSelectOption(int ProvinceId,string defaultValue)
        {
            ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
            List<IDNameObj> listOfData = bllArea.QueryCities(ProvinceId);
            return BLLHelper.GenerateToSelectOption(listOfData, defaultValue);
        }
        #endregion


        #region "保存区域信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 保存区域信息；
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ReturnValue SaveArea(Areas a)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"保存失败！");
            //检查名字是否重复；
            if (dalArea.QueryList(1, "AreaName ='" + a.AreaName + "' and AreaID<>" + a.AreaID, "") != null)
            {
                ret.Message = "区域名称重复!";
                return ret;
            }
            int result = 0;
            if (a.AreaID > 0)
            {
                Areas o = dalArea.GetModel(a.AreaID);
                a.CreateDate = o.CreateDate;
                a.LastModifyDate = DateTime.Now;
                result = dalArea.Update(a);
            }
            else
            {
                a.LastModifyDate = DateTime.Now;
                a.CreateDate = DateTime.Now;
                result = dalArea.Add(a);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "删除一条指定的区域信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 删除一条指定的区域信息；
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        public ReturnValue DeleteArea(int areaId)
        {
            ReturnValue ret = new ReturnValue();
            if (dalArea.Delete(areaId) > 0)
            {
                ret.Message = "删除失败！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询一条指定的区域信息  by 赵国伟  2012-04-02"
        /// <summary>
        /// 查询一条指定的区域信息
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        public AreaInfo GetArea(int areaId)
        {
            Areas source = dalArea.GetModel(areaId);
            AreaInfo a = new AreaInfo();
            a.Area = source;
            a.CityName = DALHelper.GetFieldValue<Cities>(source.CityID, "CityName");
            a.Province = dalProvince.Find(source.CityID);
            return a;
        }
        #endregion

        #region "分页查询指定条件下的区域信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 分页查询指定条件下的区域信息；
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="cityId"></param>
        /// <param name="ProvinceId"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<AreaInfo> QueryAreasAsPage(string keyWords, int cityId, int ProvinceId, AsPage p)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(keyWords))
                cond += " and AreaName like '%" + keyWords + "%' ";
            if (cityId > 0)
                cond += " and CityID=" + cityId;
            if (ProvinceId > 0)
                cond += " and CityID in(Select CityID From Cities Where ProvinceID=" + ProvinceId + ") ";
            string orderby = "CityID,OrderValue,AreaID desc";
            List<Areas> listOfSource = dalArea.QueryListAsPage(cond, orderby, p.CurPage, p.PageSize, out p.TotalCount);
            List<AreaInfo> listOfTarget = null;
            if (listOfSource != null)
            {
                List<string> listOfCityIds = new List<string>();
                foreach (Areas source in listOfSource)
                {
                    if (listOfCityIds.IndexOf(source.CityID.ToString()) < 0)
                        listOfCityIds.Add(source.CityID.ToString());
                }

                listOfTarget = new List<AreaInfo>();
                Dictionary<int, string> dictProvinces = QueryProvincesToHash(); //省份ID与省份名称对应关系；
                Dictionary<int, string> dictCities = QueryCitiesToHash(listOfCityIds); //城市ID与城市名称对应关系；
                Dictionary<int, int> dictCityProvinceMap = QueryCityProvinceMapToHash(listOfCityIds); //城市ID与省份ID对应关系。
                foreach (Areas source in listOfSource)
                {
                    AreaInfo target = new AreaInfo();
                    target.Area = source;
                    target.CityName = dictCities[source.CityID];
                    target.Province = new IDNameObj();
                    target.Province.ID = dictCityProvinceMap[source.CityID];
                    target.Province.Name = dictProvinces[target.Province.ID];
                    listOfTarget.Add(target);
                }
            }
            return listOfTarget;
        }
        #endregion

        #region "查询指定城市下的区域信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 查询指定城市下的区域信息；
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryArea(int cityId)
        {
            string cond = "CityID=" + cityId;
            return dalArea.Find(cond, "OrderValue,AreaID desc");
        }
        #endregion

        #region "查询指定城市下的区域并输出为下拉选项； by 赵国伟 2012-04-22"
        /// <summary>
        /// 查询指定城市下的区域并输出为下拉选项；
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryAreasToSelectOption(int cityId, string defaultValue)
        {
            ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
            return BLLHelper.GenerateToSelectOption(bllArea.QueryArea(cityId), defaultValue);
        }
        #endregion


       


    }

}
